package com.zsule.controllers.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zsule.constants.CoreConstant;
import com.zsule.controllers.BaseController;
import com.zsule.entity.EUser;
import com.zsule.entity.req.ResetPwdParams;
import com.zsule.service.IUserService;
import com.zsule.utils.security.MD5;

@Controller
// @RequestMapping("/euser")
public class UserInfoController extends BaseController {

	@Autowired
	private IUserService euserService;

	@RequestMapping("/ucenter")
	public ModelAndView ucenter(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/euser/ucenter.httl");
		EUser user = (EUser) this.getSessionAttribute(request, CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			return new ModelAndView("login.httl");
		}
		mav.addObject("user", user);
		mav.addObject("leaf", "center");
		return mav;
	}

	@RequestMapping("/euser/bindEmail")
	public ModelAndView bindEmail(HttpServletRequest request, HttpServletResponse response, EUser euser) {
		System.out.println(euser.getEmail());
		ModelAndView mav = new ModelAndView("redirect:/ucenter");
		EUser user = (EUser) this.getSessionAttribute(request, CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			return new ModelAndView("login.httl");
		}
		euserService.bindEmail(user.getId(), euser.getEmail());
		user.setEmail(euser.getEmail());
		this.setSessionAttribute(request, response, CoreConstant.USER_SESSION_NAME, user);
		mav.addObject("user", user);
		mav.addObject("leaf", "center");
		return mav;
	}

	@RequestMapping("/euser/resetPwd")
	// 页面不用变
	public ModelAndView resetPwd(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/euser/resetPwd.httl");
		EUser user = (EUser) this.getSessionAttribute(request, CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		mav.addObject("user", user);
		mav.addObject("leaf", "resetPwd");
		return mav;
	}

	@RequestMapping("/euser/commitePwd")
	// 页面不用变
	public ModelAndView commitePwd(HttpServletRequest request, HttpServletResponse response, ResetPwdParams pwd) {
		ModelAndView mav = new ModelAndView("redirect:/login");
		EUser user = (EUser) this.getSessionAttribute(request, CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			return mav;
		}
		EUser reUser = euserService.getByUserName(user);
		if (MD5.getMD5(pwd.getOldPassword()).equals(reUser.getPassword())) {
			reUser.setPassword(MD5.getMD5(pwd.getNewPassword()));
			euserService.save(reUser);
			this.clear(request, response);
		} else {
			mav.setViewName("redirect:/euser/resetPwd");
			mav.addObject("oldPwdError", true);
			return mav;
		}
		return mav;
	}
}
